package cn.cvs.controller;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.VolunteerApply;
import cn.cvs.service.volunteerApply.VolunteerApplyService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/volunteer")
public class VolunteerApplyController {

    @Resource
    private VolunteerApplyService volunteerApplyService;

    @RequestMapping(value="/list")
    public String queryPageList(Model model,
                                @RequestParam(value = "status",required = false) Integer status,
                                @RequestParam(value ="pageIndex", defaultValue = "1") Integer pageIndex ) {
        //设置页面容量
        Integer pageSize = Constants.pageSize;

        int totalCount = volunteerApplyService.count(status);

        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(pageIndex);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();

        List<VolunteerApply> volunteerApplies = volunteerApplyService.queryPageList(status, pageIndex, pageSize);

        if(pageIndex > totalPageCount){
            pageIndex = totalPageCount;
        }

        model.addAttribute("volunteerList", volunteerApplies);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", pageIndex);

        return "volunteer/list";
    }

    @DeleteMapping(value="/del/{id}")
    @ResponseBody
    public Map<String,String> delete(@PathVariable(value = "id",required = true)Integer id){
        Map<String,String> resultMap = new HashMap<>();
        int result = volunteerApplyService.removeById(id);
        if (result > 0) {
            resultMap.put("result","true");
        } else {
            resultMap.put("result","false");
        }
        return resultMap;
    }

    /**
     * 修改志愿者申请信息
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/modify")
    public String modify(@RequestParam(value = "id",required = true) Integer id,
                         @RequestParam(value = "status",required = true)Integer status){
        int result = volunteerApplyService.modifyById(id, status);
        if(result <= 0){
            return "volunteer/update";
        }
        return "redirect:/sys/volunteer/list";
    }

    /**
     * 查询申请志愿者详情信息
     * @param id
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable Integer id, Model model, HttpServletRequest request){
        VolunteerApply volunteerApply = volunteerApplyService.queryById(id);
        model.addAttribute(volunteerApply);
        return "volunteer/update";
    }
}
